package huxibianjie.com.lbscandroid.ui.new_activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.up72.libzxing.zxing.encode.CodeCreator;

import org.greenrobot.eventbus.EventBus;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.manager.RouteManager;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.SuperiorInfoModel;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.Utils;

/**
 * 邀请好友
 */
public class InviteFriendActivity extends BaseActivity implements View.OnClickListener {
    private SuperiorInfoModel superiorInfoModel;

    private LinearLayout layNoInvite, layInviteSuccess;
    private TextView tvInviteAccount, tvRegisterTime, tvStruggleTime;

    private EditText etInviteCode;
    private TextView tvInviteConfirm;

    private TextView tvMyInviteCode, tvMyInviteCodeCopy, tvMy2code;

    private ImageView ivMy2code;

    private TextView tvInviteRecord;

    @Override
    protected int getContentView() {
        return R.layout.activity_invite_friend;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "邀请好友");
        layNoInvite = findViewById(R.id.lay_no_invite);
        layInviteSuccess = findViewById(R.id.lay_invite_success);
        tvInviteAccount = findViewById(R.id.tv_invite_account);
        tvRegisterTime = findViewById(R.id.tv_register_time);
        tvStruggleTime = findViewById(R.id.tv_struggle_time);
        etInviteCode = findViewById(R.id.et_invite_code);
        tvInviteConfirm = findViewById(R.id.tv_invite_confirm);
        tvMyInviteCode = findViewById(R.id.tv_my_invite_code);
        ivMy2code = findViewById(R.id.iv_my_2code);
        tvInviteRecord = findViewById(R.id.tv_invite_record);
        tvMyInviteCodeCopy = findViewById(R.id.tv_my_invite_code_copy);
        tvMy2code = findViewById(R.id.tv_my_2code);
    }

    @Override
    protected void initListener() {
        tvInviteConfirm.setOnClickListener(this);
        tvMyInviteCode.setOnClickListener(this);
        ivMy2code.setOnClickListener(this);
        tvInviteRecord.setOnClickListener(this);
        tvMyInviteCodeCopy.setOnClickListener(this);
        tvMy2code.setOnClickListener(this);
    }

    private void loadData() {
        if (superiorInfoModel != null) {
            if (superiorInfoModel.getSuperiorInfoFlag() == 1) {
                layNoInvite.setVisibility(View.GONE);
                layInviteSuccess.setVisibility(View.VISIBLE);
                tvRegisterTime.setText(String.valueOf("注册时间：" + superiorInfoModel.getCreateTime()));
                tvStruggleTime.setText(Html.fromHtml("共同奋斗：<font color='#FFFC00'>" + superiorInfoModel.getNumberDays() + "天</font>"));
                tvInviteAccount.setText(String.valueOf("邀请人账号：" + Utils.starPhone(superiorInfoModel.getPhone())));
            } else {
                layNoInvite.setVisibility(View.VISIBLE);
                layInviteSuccess.setVisibility(View.GONE);
            }

            tvMyInviteCode.setText(superiorInfoModel.getMyInvitationCode());
            Bitmap bitmap = null;
            try {
                bitmap = CodeCreator.createQRCode(superiorInfoModel.getUrl());
                ivMy2code.setImageBitmap(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void initData() {
        if (UserManager.getInstance().isLogin()) {
            showLoading();
            UserModel userModel = UserManager.getInstance().getUserModel();
            Task.create(Service.class).getSuperiorInfo(String.valueOf(userModel.getId())).enqueue(new Callback<SuperiorInfoModel>() {
                @Override
                public void onSuccess(@Nullable SuperiorInfoModel superiorInfoModel) {
                    if (superiorInfoModel != null) {
                        InviteFriendActivity.this.superiorInfoModel = superiorInfoModel;
                        loadData();
                    } else {
                        showToast("似乎已断开互联网的连接");
                    }
                    cancelLoading();
                }

                @Override
                public void onFailure(@NonNull String error) {
                    showToast(error);
                    cancelLoading();
                }
            });
        }
    }


    @Override
    public void onClick(View view) {
        ClipboardManager cmb;
        switch (view.getId()) {
            case R.id.tv_invite_record:
                RouteManager.getInstance().showInviteRecordActivity(this);
                break;
            case R.id.tv_invite_confirm:
                if (UserManager.getInstance().isLogin()) {
                    String code = etInviteCode.getText().toString();
                    if (code.length() != 6) {
                        showToast("邀请码格式错误");
                        return;
                    }
                    UserModel userModel = UserManager.getInstance().getUserModel();
                    showLoading();
                    Task.create(Service.class).bindingInvitationCode(String.valueOf(userModel.getId()), code).enqueue(new Callback<String>() {
                        @Override
                        public void onSuccess(@Nullable String data) {
                            initData();
                            EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.REFRESH_USER_INFO, null, ""));
                            new android.support.v7.app.AlertDialog.Builder(InviteFriendActivity.this).setMessage("您已绑定成功，获得1000双倍能量")
                                    .setPositiveButton("确定", null).show();
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            showToast(error);
                            cancelLoading();
                        }
                    });
                }
                break;
            case R.id.tv_my_invite_code_copy:
            case R.id.tv_my_invite_code:
                cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (cmb != null && superiorInfoModel != null && superiorInfoModel.getMyInvitationCode() != null) {
                    cmb.setText(superiorInfoModel.getMyInvitationCode());
                    showToast("邀请码已复制到剪贴板，快去分享给你的朋友吧～");
                } else {
                    showToast("邀请码复制失败");
                }
                break;
            case R.id.tv_my_2code:
            case R.id.iv_my_2code:
                cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                if (cmb != null && superiorInfoModel != null && superiorInfoModel.getInvitationUrl() != null) {
                    cmb.setText(superiorInfoModel.getInvitationUrl());
                    showToast("邀请链接已复制到剪贴板，快去分享给你的朋友吧～");
                } else {
                    showToast("邀请链接复制失败");
                }
                break;

        }
    }
}
